"""Defines a command class."""

class cmderr_t(Exception):
    pass
    

class cmd_t(object):
    """Defines a command object. You need to define some properties.

    Required:

    1. self.name -- the name to invoke.

    Optional:
    
    2. self.type -- what to respond to, either RECIEVED, SENT, SENDING,
                    or READ. This typically defaults to SENDING.
    3. self.aliases -- a list of optional command names. For example your
                       command name may be "move", but you may want to
                       provide an alias, like "mv".
    """
    
    def __init__(self, kernal):
        self.kernal = kernal
        if not hasattr(self, "short_desc"):
            self.short_desc = ""
        if not hasattr(self, "desc"):
            self.desc = ""
        if not hasattr(self, "aliases"):
            self.aliases = []
        
    def execute(self, argv):
        """Override this method. The "argv' will look just like a
        sys.argv would look like for a program execution."""
        raise NotImplementedError, "you need to define execute method"

    def match(self, name):
        """Checks the TEXT to make sure it matches against the cmd name.
        In other words the TEXT should look like "command ...."""
        if name.startswith(self.name):
            return True
        for alias in self.aliases:
            if name.startswith(alias):
                return True
        return False

    def write(self, msg):
        self.kernal.write(msg)

    def replace(self, msg):
        self.kernal.replace(msg)


    def log(self, lvl, fmt, args=()):
        # TODO - this could be done better.
        _LOG(lvl, fmt, args) 
